
var __UIMatchPlay_instance = null;
function getUIMatchPlay() { return __UIMatchPlay_instance; }

var UIMatchPlay = function(){};

UIMatchPlay.prototype.onDidLoadFromCCB = function()
{   
    cc.log("UIMatchPlay.onDidLoadFromCCB");
    __UIMatchPlay_instance = this;

    // 比赛1秒的真实时间(单位:秒)
    this.MATCH_SECOND = 0.01;

    this._data = null;
    this._is_fristhalf = true;  // 是否上半场

    // 士气进度 上限500000
    this._shiqi_a = 0;
    this._shiqi_b = 0;
    // 比赛时间 
    this._mtime = 0;
    // 攻击计数器
    this._attack_cnt = 0;
    // 比分
    this._bifen_a = 0;
    this._bifen_b = 0;
    // 球员面板
    this._player_panels = [];
    

    this.clearUI();
    //this.playVideo('res/Video/dianqiu01/dianqiu', null);
    //gnet.sendHttp('http://record.app100655540.twsapp.com/cf8334b8940f1031801e00163e6471ac_s9_09725e8000_1', function(d){getUIMatchPlay().play(d)});
};




// 播放战斗数据
UIMatchPlay.prototype.play = function(data) {
    cc.log('play');
    this._data = data;

    // 玩家数据
    this.updatePlayerInfo();

    cc.log('match type:'+data.matchtype); //普通列表项目0:其它 1:联赛 2:杯赛 3: 4:竞技场 5:boss 6:球场争夺 7:好友邀请赛 8:天梯 9:奴隶 10:新手引导 11:天王山 12:萌战
    cc.log('recid:'+data.recid); //战报ID
    cc.log('result:'+data.result); //比赛结果 {A: 1,B: 3},
    cc.log('addition:'+data.addition); //鼓舞增加的进攻防守值的百分比

    // 比分
    this._bifen_a = 0;
    this._bifen_b = 0;
    this.setBifen(this._bifen_a, this._bifen_b);

    // 时间
    this._mtime = 0;
    this.setTime(this._mtime);

    // 攻击类型
    this.hideAttackType();

    // 成功率
    this.hideRate();

    // 比赛信息
    this.updateMatchInfo();

    this.startMatch();

};


UIMatchPlay.prototype.startMatch = function() {
    cc.Director.getInstance().getScheduler().scheduleCallbackForTarget(this, this.update, this.MATCH_SECOND, cc.REPEAT_FOREVER);
};

UIMatchPlay.prototype.pauseMatch = function() {
    cc.Director.getInstance().getScheduler().pauseTarget(this);
};

UIMatchPlay.prototype.resumeMatch = function() {
    cc.Director.getInstance().getScheduler().resumeTarget(this);
};

UIMatchPlay.prototype.stopMatch = function() {
    cc.Director.getInstance().getScheduler().unscheduleCallbackForTarget(this, this.update);
};

UIMatchPlay.prototype.update = function(dtime) {
    // 士气值
    var MAX = 500000;
    if(this._is_fristhalf) {
        ma = this._data.A_F.morale;
        mb = this._data.B_F.morale;
    } else {
        ma = this._data.A_S.morale;
        mb = this._data.B_S.morale;
    }

    this._shiqi_a += ma;
    this._shiqi_b += mb;
    if(this._shiqi_a >= MAX) { this._shiqi_a = 0; }
    if(this._shiqi_b >= MAX) { this._shiqi_b = 0; }

    //cc.log('shiqi: a:'+this._shiqi_a + '   b:'+this._shiqi_b);
    this.setShiqitiao( this._shiqi_a/MAX, this._shiqi_b/MAX );

    // 设置时间
    this.setTime(this._mtime);

    // 发动进攻
    this.checkAttack(this._mtime);

    // 比赛时间过去1秒
    this._mtime += 1;
    if ( this._is_fristhalf && this._mtime > 2700 ) { // 2700:45分钟
        // 切换半场
        this._is_fristhalf = false;
        this.updateMatchInfo();
    }

    if ( this._mtime > 5400 ) { // 90分钟
        this.stopMatch();
        // 退出比赛
        this.onClose();
    }
};


// 更新界面
UIMatchPlay.prototype.updatePlayerInfo = function() {
    var d = this._data.common; // 基础信息

    this.uiLTeam.setString(d.A_mname);
    this.uiLPlayer.setString(d.A_tname);
    this.setHead(this.uiLHead, d.A_icon);

    this.uiRTeam.setString(d.B_mname);
    this.uiRPlayer.setString(d.B_tname);
    this.setHead(this.uiRHead, d.B_icon);
    
    // d.A_lv
    // d.A_currinfo ??
};

UIMatchPlay.prototype.setHead = function(sp, data) {
    if(isNaN(parseInt(data))) {
        sp.init('res/Misc/any.png');
    } else {
        sp.init('res/icon/badge/club_'+data+'_normal.png');
    }
};

UIMatchPlay.prototype.updateMatchInfo = function() {

    if(this._is_fristhalf) {
        da = this._data.A_F;
        db = this._data.B_F;
    } else {
        da = this._data.A_S;
        db = this._data.B_S;
    }

    this.uiLShili.setString("实力:"+parseFloat(da.power).toFixed(2));
    this.uiLZhanshu.setString("战术:"+da.tactic);
    this.uiLZhenxing.setString("阵型:"+da.matrix);
    this.uiLShiqi.setString(""+parseInt(da.morale));

    this.uiRShili.setString("实力:"+parseFloat(db.power).toFixed(2));
    this.uiRZhanshu.setString("战术:"+db.tactic);
    this.uiRZhenxing.setString("阵型:"+db.matrix);
    this.uiRShiqi.setString(""+parseInt(db.morale));

    // 上下半场提示
    this.uiShangbanchang.setVisible(this._is_fristhalf);
    this.uiXiabanchang.setVisible(!this._is_fristhalf);  
};

UIMatchPlay.prototype.setShiqitiao = function(pa, pb) {
    this.uiLShiqitiao.setScaleX(pa);
    this.uiRShiqitiao.setScaleX(pb);
};


UIMatchPlay.prototype.setTime = function(t) {
    mm = parseInt(t / 60);
    ss = t - mm * 60;
    this.uiTimer.setString(glb.formatNumber('00',mm) + ':' + glb.formatNumber('00',ss));
};

UIMatchPlay.prototype.setBifen = function(a, b) {
    this.uiBifen.setString(a + ' : ' + b);
};



// 是否发动进攻
UIMatchPlay.prototype.checkAttack = function(match_time) {

    next_attack = this._data.round_context[this._attack_cnt];
    if(!next_attack) return;

    if (match_time >= next_attack.timestamp[0]) {
        this.doAttack(next_attack);
        this._attack_cnt += 1;
    }
};

// 发动攻击
UIMatchPlay.prototype.doAttack = function(round_data) {
    d = round_data;
    
    cc.log('attakc_type:'+d.attack_type); // 1,头球 2,远射 3,推射 4,抢点 5,吊射 6,单刀 7,任意球 8,角球 9,点球
    // d.S_needplayer 射门阶段出现的球员
    // d.breakrate: -1, 突破阶段成功率 如果为-1表该没有突破阶段
    // d.timestamp 进攻发生的时间和球队
    // d.isgoal: true, 是否进球
    cc.log('now_attack:'+d.now_attack); // 发启进攻的球队
    // d.B_needplayer: { }, 突破阶段需要的球员
    // d.shotrate: 射门成功率 [原始成功率,进攻方加成,防守方减成]

    // 暂停时间
    this.pauseMatch();


    // 判断是否有突破阶段
    if(d.breakrate != -1) {
        // 突破
        cc.log('突破');
        this.doAttackBreak(d);
    } else {
        this.doAttackShoot(d);
    }

};

UIMatchPlay.prototype.doAttackBreak = function(round_data) {

    this.showAttackType('突破');
    // 显示球员信息
    this.showNeedPlayer(round_data.B_needplayer);
    // 显示成功率
    this.showRate(round_data.breakrate[0]);
    // 播放特效
    FXManager.playAnimation('FX1/me2', this.uiRateBg, -10, 20, 0.05, false, _breakAni );

    // 播放突破动画
    function _breakAni() {
        cc.log("_breakAni");
        _this = getUIMatchPlay();
        _this.hideAttackType();
        _this.hideNeedPlayer();
        _this.hideRate();

        _this.playVideo('res/Video/dianqiu01/dianqiu', function () { _this.doAttackShoot(round_data); } );
    };
};

UIMatchPlay.prototype.doAttackShoot = function(round_data) {

    this.showAttackType('射门');
    // 显示球员信息
    this.showNeedPlayer(round_data.S_needplayer);
    // 显示成功率
    this.showRate(round_data.shotrate[0]);
    // 播放特效
    FXManager.playAnimation('FX1/me2', this.uiRateBg, -10, 20, 0.05, false, _shootAni );

    // 播放突破动画
    function _shootAni() {
        cc.log("_shootAni");
        _this = getUIMatchPlay();
        _this.hideAttackType();
        _this.hideNeedPlayer();
        _this.hideRate();

        _this.playVideo('res/Video/dianqiu01/dianqiu', function () { _this.resumeMatch(); } );

        // 更新比分
        if(round_data.isgoal) {
            if (round_data.now_attack == 'A') { _this._bifen_a += 1; }
            else { _this._bifen_b += 1; }
            _this.setBifen(_this._bifen_a, _this._bifen_b);
        }
    };
};

UIMatchPlay.prototype.showNeedPlayer = function(need_player_data) {

    function _create(d, parent, xypos) {
        cc.log('skills:' + d.skils);      // 有效果的技能
        cc.log('pos:' + d.pos);           // 比赛中的位置
        cc.log('sav:' + d.sav);           // 除去 pos skills name 后的其它key 均表示在该次进攻中所需要的关键属性
        cc.log('name:' + d.name);         // 名字
        cc.log('air:' + d.air);
        cc.log('this:' + this);
        return createUIMatchPlayerPanel(parent, xypos, 'res/Misc/any.png', d.name, helper.getMatchPositionString(d.pos), '属性1', '属性2');
    }

    if(need_player_data.A1) {
       this._player_panels.push( _create(need_player_data.A1, this.uiPlayerPanels, cc.p(156,166)) );
    }
    
    if (need_player_data.A2) {
        this._player_panels.push( _create(need_player_data.A2, this.uiPlayerPanels, cc.p(156,104)) );
    }

    if (need_player_data.G) {
        this._player_panels.push( _create(need_player_data.G, this.uiPlayerPanels, cc.p(330,44)) );
    }

    if (need_player_data.B) {
        y = 44;
        if(need_player_data.G) { y += 62; }
        this._player_panels.push( _create(need_player_data.B, this.uiPlayerPanels, cc.p(324,y)) );
    }
};

UIMatchPlay.prototype.hideNeedPlayer = function() { 
    for each(var p in this._player_panels) {
        this.uiPlayerPanels.removeChild(p);
    }
    this._player_panels = [];
};

UIMatchPlay.prototype.showAttackType = function(text) {
    this.uiAttackTypeBg.setVisible(true);
    this.uiAttackType.setVisible(true);
    this.uiAttackType.setString(text);
};

UIMatchPlay.prototype.hideAttackType = function() {
    this.uiAttackTypeBg.setVisible(false);
    this.uiAttackType.setVisible(false);
};


UIMatchPlay.prototype.showRate = function(rate) {
    this.uiRateBg.setVisible(true);
    this.uiRate.setString(parseInt(rate * 100) + '%');
};

UIMatchPlay.prototype.hideRate = function() {
    this.uiRateBg.setVisible(false);
};

// 清理UI显示
UIMatchPlay.prototype.clearUI = function() {
    this.setTime(0);
    this.setBifen(0,0);
    this.setShiqitiao(0,0);

    this.hideNeedPlayer();
    this.hideAttackType();
    this.hideRate();
};


UIMatchPlay.prototype.playVideo = function(name, callback) {
    FXManager.playVideo(name, this.uiVideoLayer, 240, 106, callback);
};

// 更新界面
UIMatchPlay.prototype.onClose = function()
{
    //* 清理数据
    this.stopMatch();
    getUIMain().closeUIMatchPlay();
    
};



